using System;
using System.Collections.Generic;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.Text;

namespace Sinacor.Infra.Service.Security.Services
{
    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage((AttributeTargets.Class | AttributeTargets.Interface))]
    public sealed class SinacorServiceBehaviorAttribute : Attribute, IServiceBehavior
    {
        #region Public Methods

        #region IServiceBehavior Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        /// <param name="endpoints"></param>
        /// <param name="bindingParameters"></param>
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            string mexContractName = typeof(IMetadataExchange).Name;

            foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher endpoint in dispatcher.Endpoints)
                {
                    if (endpoint.ContractName != mexContractName)
                        endpoint.DispatchRuntime.MessageInspectors.Add(new SinacorServiceBehaviorDispatchMessageInspector());
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }

        #endregion

        #endregion
    }
}
